//
//  LXRightCell.m
//  灵秀
//
//  Created by 李田 on 17/3/18.
//  Copyright © 2017年 tianyao. All rights reserved.
//

#import "LXRightCell.h"
#import "LXFoodDetailModel.h"
#import "LXMinusPlusView.h"
@interface LXRightCell ()<LXMinusPlusViewDelegate>

//图标
@property (nonatomic, weak) UIImageView *iconView;
//菜名
@property (nonatomic, weak) UILabel *nameLabel;
//月售
@property (nonatomic, weak) UILabel *saleLabel;
//点赞数
@property (nonatomic, weak) UILabel *praiseLabel;
//价格
@property (nonatomic, weak) UILabel *priceLabel;
//描述
@property (nonatomic, weak) UILabel *desLabel;
//加减号
@property(nonatomic, weak)LXMinusPlusView *minusPlusView;

@end

@implementation LXRightCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 3.0 实现加减号代理方法
- (void)minusPlusView:(LXMinusPlusView *)minusV withCount:(NSInteger)minusCount {
    
    // 接收加减号的数量修改模型中的个数
    _model.buyCount = minusCount;
    
    // 获取主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // cell上的图片相对于主窗口的位置
    CGPoint iconPoint = [self convertPoint:self.iconView.center toView:keyWindow];
    
    if (_minusPlusView.isPlus) {
        
        // 将cell的数据 传递给控制器
        if([_delegate respondsToSelector:@selector(rightCell:withPlusModel: withIconPoint:)]){
            
            [_delegate rightCell:self withPlusModel:_model withIconPoint:iconPoint];
        }
    } else {
        
        if([_delegate respondsToSelector:@selector(rightCell:withMinusModel:)]){
            
            [_delegate rightCell:self withMinusModel:_model];
        }
    }
}

#pragma mark - 2.0给控件赋值
- (void)setModel:(LXFoodDetailModel *)model{
    
    _model = model;
    
    //  图片
    NSString *urlStr = model.picture;
    // 删除尾缀--拓展名
    urlStr = [urlStr stringByDeletingPathExtension];
    NSURL *url = [NSURL URLWithString:urlStr];
    // 请求图片并显示
    [_iconView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"xiaolian"]];
    // 菜名
    _nameLabel.text = model.name;
    // 月售
    _saleLabel.text = model.month_saled_content;
    // 点赞数
    _praiseLabel.text = model.praise_num.description;
    // 价格
    _priceLabel.text = [NSString stringWithFormat:@"¥%@", model.min_price] ;
    // 描述
    _desLabel.text = model.Description;
    // 购买数量
    _minusPlusView.minusCount = model.buyCount;
}

#pragma mark - 1.0 搭建界面
- (void)setupUI {
    
    CGFloat margin = 10;
    
    //取消cell的选中样式
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    //1.icon
    UIImageView *iconV = [[UIImageView alloc]init];
    //填充方式--图片不变形
    iconV.contentMode = UIViewContentModeScaleAspectFill;
    iconV.layer.cornerRadius = 5;
    iconV.layer.masksToBounds = YES;
    [self.contentView addSubview:iconV];
    
    //2.菜名
    UILabel *nameL  = [UILabel cz_labelWithText:@"天堂饭" fontSize:13 color:[UIColor blackColor]];
    [self.contentView addSubview:nameL];
    
    //3.月售
    UILabel *saleL  = [UILabel cz_labelWithText:@"月售998" fontSize:11 color:[UIColor cz_colorWithHex:0x7e7e7e]];
    [self.contentView addSubview:saleL];
    
    //4.点赞图片
    UIImage *praiseImage = [UIImage imageNamed:@"icon_food_review_praise"];
    UIImageView *praiseV = [[UIImageView alloc]init];
    praiseV.image = praiseImage;
    [self.contentView addSubview:praiseV];
    
    //5.点赞数
    UILabel *praiseL  = [UILabel cz_labelWithText:@"90" fontSize:11 color:[UIColor cz_colorWithHex:0x7e7e7e]];
    [self.contentView addSubview:praiseL];
    
    //6.价格
    UILabel *priceL  = [UILabel cz_labelWithText:@"¥2.9" fontSize:14 color:[UIColor cz_colorWithHex:0xfa2a09]];
    [self.contentView addSubview:priceL];
    
    //7.描述
    UILabel *desL  = [UILabel cz_labelWithText:@"精选东北大米,特级黑芝麻" fontSize:11 color:[UIColor cz_colorWithHex:0x848484]];
    [self.contentView addSubview:desL];
    
    //    MARK:8.加减按钮
    
    LXMinusPlusView *minusPlusView = [LXMinusPlusView minusPlusView];
    [self.contentView addSubview:minusPlusView];
    
    //    MARK: 代理
    minusPlusView.delegate = self;
    
    //    MARK:关联
    _iconView = iconV;
    _nameLabel = nameL;
    _saleLabel = saleL;
    _praiseLabel = praiseL;
    _priceLabel = priceL;
    _desLabel = desL;
    _minusPlusView = minusPlusView;
    
    //MARK:布局
    [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(margin);
        make.left.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iconV.mas_top);
        make.left.equalTo(iconV.mas_right).offset(margin);
    }];
    
    [saleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(nameL);
        make.top.equalTo(nameL.mas_bottom).offset(margin/2);
    }];
    
    [praiseV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.baseline.equalTo(saleL);
        make.left.equalTo(saleL.mas_right).offset(margin/2);
        make.size.mas_equalTo(praiseImage.size);
    }];
    
    
    [praiseL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.baseline.equalTo(saleL);
        make.left.equalTo(praiseV.mas_right).offset(margin/2);
    }];
    
    [priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(saleL);
        make.top.equalTo(saleL.mas_bottom).offset(margin/2);
    }];
    
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iconV.mas_bottom).offset(16);
        make.left.equalTo(iconV);
        make.right.equalTo(self.contentView).offset(-margin);
        make.bottom.equalTo(self.contentView).offset(-margin);
    }];
    
    [minusPlusView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView).offset(-margin);
        make.bottom.equalTo(priceL.mas_bottom);
        make.size.mas_equalTo(minusPlusView.bounds.size);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
